import { message } from 'antd';
import {
  getPreAuthUrl,
  getAuthorizerList,
  getAuthorizer,
  getMediaList,
  sendTextMsgSchedule,
  sendMediaMsgSchedule,
  sendImgUrlMsgSchedule,
  queryCustomerTask,
} from '../services/wx';

export default {
  namespace: 'customer_message',

  state: {
    authorizerList: [],
    mediaList: [],
    schedules: [],
  },

  effects: {
    // 查询已授权公众号列表
    *fetchAuthorizerList(_, { call, put }) {
      const res = yield call(getAuthorizerList);
      yield put({
        type: 'saveAuthorizerList',
        payload: res,
      });
    },
    // 模糊查询已授权公众号
    *fetchAuthorizer({ payload }, { call, put }) {
      const res = yield call(getAuthorizer, payload);
      yield put({
        type: 'saveAuthorizerList',
        payload: res,
      });
    },
    // 授权公众号
    *addGH(_, { call, put }) {
      const res = yield call(getPreAuthUrl);
      yield put({
        type: 'gotoPreAuthUrl',
        payload: res,
      });
    },
    // 查询某个公众号里的素材列表
    *fetchMediaList({ payload }, { call, put }) {
      const res = yield call(getMediaList, payload);
      if (res) {
        yield put({
          type: 'saveMediaList',
          payload: res,
        });
      } else {
        message.error('获取失败,请检查公众号后台');
      }
    },
    // 创建文本客服消息任务
    *createTextMsgSchedule({ payload }, { call }) {
      const res = yield call(sendTextMsgSchedule, payload);
      if (res === 'ok') {
        message.success('操作成功');
      }
    },
    // 创建素材客服消息任务
    *createMediaMsgSchedule({ payload }, { call }) {
      const res = yield call(sendMediaMsgSchedule, payload);
      console.log(res);
      // if (res === 'ok') {
      //   message.success('操作成功');
      // }
    },
    // 创建图片链接消息任务
    *createImgUrlMsgSchedule({ payload }, { call }) {
      console.log(payload);
      const res = yield call(sendImgUrlMsgSchedule, payload);
      console.log(res);
    },
    // 查询客服消息定时任务
    *fetchSchedules(_, { call, put }) {
      const res = yield call(queryCustomerTask);
      yield put({
        type: 'saveSchedules',
        payload: res,
      });
    },
    // 删除客服消息定时任务
    *deleteSchedule({ payload }, { call, put }) {
      // const res = yield call(deleteTask, payload);
      // if (res === 'ok') {
      //   message.success('删除成功');
      //   yield put({
      //     type: 'fetchSchedules',
      //   });
      // } else {
      //   message.error('删除失败');
      // }
    },
  },

  reducers: {
    saveAuthorizerList(state, { payload }) {
      return {
        ...state,
        authorizerList: payload,
      };
    },
    gotoPreAuthUrl(state, { payload }) {
      window.open(payload, '', 'height=600, width=800, top=100, left=300');
      return { ...state };
    },
    cleanMediaList(state, _) {
      return {
        ...state,
        mediaList: [],
      };
    },
    saveMediaList(state, { payload }) {
      return {
        ...state,
        mediaList: payload,
      };
    },
    // 保存任务列表
    saveSchedules(state, { payload }) {
      return {
        ...state,
        schedules: payload,
      };
    },
  },
};
